from tools.my_methods import *


class Solution:
    def monotoneIncreasingDigits(self, N: int) -> int:
        data, x = [], N
        while x:
            data.append(x % 10)
            x //= 10
        data.reverse()
        for i in range(1, len(data)):
            if data[i] < data[i-1]:
                j = i-1
                while j > 0 and data[j-1] == data[j]:
                    j -= 1
                data[j] -= 1
                for k in range(j+1, len(data)):
                    data[k] = 9
                break
        ans = 0
        for x in data:
            ans = ans * 10 + x
        return ans


questions = [
    (10,),
    (1234,),
    (332,),
    (668841,),
]

work(questions, lambda q: Solution().monotoneIncreasingDigits(q[0]))
